package com.hc.pokemen.feature_pokemen.presentation.pokemon_list.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.SubcomposeAsyncImage

import coil.request.ImageRequest
import com.hc.pokemen.feature_pokemen.domain.entity.PokemonListItem
import com.hc.pokemen.feature_pokemen.presentation.pokemon_list.OnPokemonListScreenEvent
import com.hc.pokemen.feature_pokemen.presentation.pokemon_list.PokemonListScreenEvent
import com.hc.pokemen.feature_pokemen.presentation.pokemon_list.PokemonListScreenState
import com.hc.pokemen.feature_pokemen.presentation.util.calcDominantColor


@Composable
fun PokemonList(state: PokemonListScreenState, onEvent: OnPokemonListScreenEvent) {
    LazyVerticalGrid(
        contentPadding = PaddingValues(horizontal = 16.dp),
        columns = GridCells.Fixed(2),
        verticalArrangement = Arrangement.spacedBy(10.dp),
        horizontalArrangement = Arrangement.spacedBy(10.dp)
    ) {
        itemsIndexed(state.pokemonList) { index, item ->
            PokemonItem(item = item, onEvent = onEvent)
            if (index >= state.pokemonList.size - 1 && !state.endReached && !state.isSearching) {
                LaunchedEffect(Unit) {
                    onEvent(PokemonListScreenEvent.PokemonListLoadMore)
                }
            }
        }
//        if (state.endReached) {
//            item(span = {
//                GridItemSpan(2)
        //                Text(text = "没有更多数据了", modifier = Modifier.fillMaxWidth().background(Color.Cyan))
//            }
//        }
    }
    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        if (state.isLoading) {
            CircularProgressIndicator(color = MaterialTheme.colorScheme.primary)
        }
        if (state.loadError.isNotEmpty()) {
            RetrySection(error = state.loadError) {
                onEvent(PokemonListScreenEvent.ErrorRetryClick)
            }
        }
    }
}

@Composable
fun PokemonItem(item: PokemonListItem, onEvent: OnPokemonListScreenEvent) {
    val defaultDominantColor = MaterialTheme.colorScheme.surface
    var dominantColor by remember {
        mutableStateOf(defaultDominantColor)
    }
    Box(
        modifier = Modifier
            .shadow(5.dp, RoundedCornerShape(10.dp))
            .clip(RoundedCornerShape(10.dp))
            .aspectRatio(1f)
            .background(Brush.verticalGradient(listOf(dominantColor, defaultDominantColor)))
            .clickable {
                onEvent(PokemonListScreenEvent.PokemonItemClick(item.name, dominantColor.toArgb()))
            },
        contentAlignment = Alignment.Center
    ) {


        Column {

            //替换CoinImage
            SubcomposeAsyncImage(
                modifier = Modifier
                    .size(120.dp)
                    .align(Alignment.CenterHorizontally),
                model = ImageRequest.Builder(LocalContext.current)
                    .data(data = item.imageUrl)
                    .build(),
                contentDescription = null,
                loading = {
                    CircularProgressIndicator(
                        color = MaterialTheme.colorScheme.primary,
                        modifier = Modifier.scale(0.5f)
                    )
                },
                onSuccess = { state ->
                    calcDominantColor(state.result.drawable) { color ->
                        dominantColor = color
                    }
                }
            )

            Text(
                text = item.name,
                modifier = Modifier.fillMaxWidth(),
                textAlign = TextAlign.Center,
                fontSize = 20.sp
            )
        }
    }
}